# coding: utf8
from nyawc.helpers.MongoHelper import mongoUtil
from nyawc.helpers.URLHelper import URLHelper
from nyawc.Queue import Queue

class UrlFilter(object):
    def __init__(self, url, method="get"):
        self.same_count = 5 # number query appear times at least
        self.same_len = 10  # string query's length at least

        self.url = url
        self.host = URLHelper.get_host(url)
        self.path = URLHelper.get_path(url)
        self.query = URLHelper.get_ordered_params(url)
        self.method = method
        self.mongo = mongoUtil(self.host)
        #self.child = self.get_child(url, method)


    def is_static(self):
        static = ['js', 'css', 'xml', 'jpg', 'gif', 'png', 'exe', 'zip', 'rar', 'ico',
                  'gz', '7z', 'tgz', 'bmp', 'pdf', 'avi', 'mp3', 'mp4']

        ext = self.path.split('.')[-1]
        return ext in static

    def is_real_static(self):
        static = ['jpg', 'gif', 'png', 'exe', 'zip', 'rar', 'ico',
                  'gz', '7z', 'tgz', 'bmp', 'pdf', 'avi', 'mp3', 'mp4']

        ext = self.path.split('.')[-1]
        return ext in static

    def get_child(self, url, method):
        condition = {
            "parent_url": url,
            "parent_method": method,
        }
        child = self.mongo.mongo_find(condition)

        return child

    def check_url(self):
        if self.is_static():
            return False
        ret = self.check_path() and self.check_query()
        condition = {
            "url": self.url,
            "method": self.method,
        }
        if ret:
            self.mongo.mongo_update(condition,{'need_to_scan': True})
        return ret

    def check_path(self):
        if self.path=="" or self.path[-1]=="/":
            return True
        last_path = URLHelper.split_path(self.url)[-1]
        if last_path.find(".") > 0: # if url has ext then not check.
            return True
        else:
            return False

    def check_query(self):
        '''Check URL weather need to be scanned.

            Args:
                request_list(list): Request list has the same method and path as the INIT_URL.

            Return:
                Need or not(bool)
        '''
        condition = {
            "path": self.path,
            "method": self.method,
        }
        request_list = self.mongo.mongo_find(condition)

        same_query_list = [] # get a list with the similar URL
        exist_scan = False
        for request in request_list:
            query = URLHelper.get_ordered_params(request["url"])
            if query.keys()==self.query.keys():
                request["query"] = query
                same_query_list.append(request)
                if request["need_to_scan"]:
                    exist_scan = True

        if not exist_scan: # to make sure that the set of the similar URL exist one to be scanned.
            return True

        key_important = [] # try to get important keys.
        for key in self.query.keys():
            count = 0
            value = self.query[key]

            if value.isdigit(): # operation for number query
                for request in same_query_list:
                    if request["query"][key]==value:
                        count += 1
                if count > self.same_count:
                    key_important.append(key)
            else:
                if len(value) < self.same_len:
                    key_important.append(key)
        if len(key_important)==0:
            return False

        for request in same_query_list:
            for key in key_important:
                if self.query[key]==request["query"][key] and request["need_to_scan"]:
                    return False
        return True
